//
//  CustomerScrollerView.m
//  UIScrollView循环滚动
//
//  Created by mac on 2017/8/2.
//  Copyright © 2017年 xiaopeng. All rights reserved.
//

#import "CustomerScrollerView.h"


 
@implementation CustomerScrollerView
- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        //
        [self initializeDataSource];
        [self initializeUserInterface];
        
    }
    return self;
}
//初始化数据源
-(void)initializeDataSource{
    
    _currentIndex = 0;
    
    _imageNameList = [[NSMutableArray alloc]init];
    
    //初始化图片
    for(NSInteger i=0;i<_totalImage;i++){
        NSString *imageName = [NSString stringWithFormat:@"guidePage_%ld",(long)i];
        [_imageNameList addObject:imageName];
    }
}
- (NSMutableArray *)imageNameList{

    if (!_imageNameList) {
        _currentIndex = 0;
        _imageNameList = [[NSMutableArray alloc]init];
    }
    return _imageNameList;
}
//初始化用户界面
-(void)initializeUserInterface{
    [self setBackgroundColor:[UIColor whiteColor]];
    
    CGFloat VIEW_WIDTH = CGRectGetWidth(self.bounds);
    CGFloat VIEW_HEIGHT = CGRectGetHeight(self.bounds);
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, VIEW_HEIGHT)];
    [_scrollView setDelegate:self];
    [_scrollView setBounces:NO];
    [_scrollView setShowsHorizontalScrollIndicator:NO];
    [_scrollView setContentSize:CGSizeMake(VIEW_WIDTH*3, VIEW_HEIGHT)];
    [_scrollView setPagingEnabled:YES];
    [_scrollView setContentOffset:CGPointMake(VIEW_WIDTH, 0)];
    [self addSubview:_scrollView];
    
    
    //初始化三个UIImageView，并固定他们位于_scrollView中的位置
    _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
    //[_leftImageView setContentMode:UIViewContentModeScaleToFill];
    
    _currentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(VIEW_WIDTH, 0, VIEW_WIDTH, VIEW_HEIGHT)];
    [_currentImageView setContentMode:UIViewContentModeScaleToFill];
    
    _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(VIEW_WIDTH*2, 0, VIEW_WIDTH, VIEW_HEIGHT)];
    [_rightImageView setContentMode:UIViewContentModeScaleToFill];
    
    //用UIImage的图像来初始化三个ImageView;
    [self initImages];
    
    
    [_scrollView addSubview:_leftImageView];
    [_scrollView addSubview:_currentImageView];
    [_scrollView addSubview:_rightImageView];
    
    
    //初始化_pageControll
    _pageControll = [[UIPageControl alloc]initWithFrame:CGRectMake(0, VIEW_HEIGHT, VIEW_WIDTH, 30)];
    [_pageControll setNumberOfPages:_totalImage];
    [_pageControll setCurrentPage:_currentIndex];
    [_pageControll setEnabled:NO];
    [_pageControll setCurrentPageIndicatorTintColor:[UIColor blackColor]];
    [_pageControll setPageIndicatorTintColor:[UIColor grayColor]];
    
    [self addSubview:_pageControll];
    
    
    //实例化_timer;
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(processTimer) userInfo:nil repeats:YES];
    
}
-(void)initImages{
    //根据当前的_currentIndex（当前需要显示的索引）来初始化三个ImageView对应的图片
    [_leftImageView setImage:[UIImage imageNamed:_imageNameList[(_currentIndex-1+_totalImage)%_totalImage]]];
    [_currentImageView setImage:[UIImage imageNamed:_imageNameList[_currentIndex]]];
    [_rightImageView setImage:[UIImage imageNamed:_imageNameList[(_currentIndex+1)%_totalImage]]];
}




-(void)updateUI{//核心函数
    
    CGFloat VIEW_WIDTH = CGRectGetWidth(self.bounds);
    CGFloat Offset_x = _scrollView.contentOffset.x;
    //判断是否需要更新界面
    BOOL isUpdate = NO;
    
    
    if(Offset_x==2*VIEW_WIDTH){
        isUpdate = YES;
        _currentIndex = (_currentIndex+1)%_totalImage;
    }
    else if(Offset_x==0){
        isUpdate = YES;
        _currentIndex = (_currentIndex-1+_totalImage)%_totalImage;
    }
    //仅仅前面的if和else if便能够实现完整的循环滚动
    
    
    //后面这两个else if和最后的else实现的功能是：当你页面划过一半时，_pageControll的当前页就立马换到上一页或者下一页，就不需要等滑动完才看到_pageControll更新。
    else if (Offset_x<=0.5*VIEW_WIDTH){
        //_pageControll更新到上一页
        [_pageControll setCurrentPage:(_currentIndex-1+_totalImage)%_totalImage];
    }
    else if (Offset_x>=1.5*VIEW_WIDTH){
        //_pageControll更新到下一页
        [_pageControll setCurrentPage:(_currentIndex+1)%_totalImage];
    }
    
    //页面滑动未达到一半时，需要将_pageControll设置为当前索引页。有人可能会觉得设置为当前页是一个多余的操作，因为他们会想，页面都没划过一半，说明_pageControll就没改变过，为什么还要设置为当前页呢。原因是：当一个人将页面划过一半后，并未松手，他又划回低于一半页面时松手。这时，上面的两个else if()当中的一个将会触发，从而引起_pageControll当前页发生改变，所以我们需要做一个[_pageControll setCurrentPage:_currentIndex]来恢复当前的_pageControll显示页，这样做就能防止页面划过一半后又划回的操作。
    
    //又介于以下两个操作执行代码均相同，并且恰好是所有剩余的情况，所以我将其合并在else中执行。
    //为了理解，我也将以下else if()代码贴出并注释掉
    
    /*
     else if (Offset_x>0.5*VIEW_WIDTH&&Offset_x<VIEW_WIDTH){
     [_pageControll setCurrentPage:_currentIndex];
     }
     else if (Offset_x>VIEW_WIDTH&&Offset_x<1.5*VIEW_WIDTH){
     [_pageControll setCurrentPage:_currentIndex];
     }
     */
    else{
        [_pageControll setCurrentPage:_currentIndex];
    }
    
    
    
    if(!isUpdate){
        return;
    }
    
    [self initImages];
    [_scrollView setContentOffset:CGPointMake(VIEW_WIDTH, 0)];
    [_pageControll setCurrentPage:_currentIndex];
    
    
}

-(void)processTimer{
    [_scrollView setContentOffset:CGPointMake(2*CGRectGetWidth(self.bounds), 0) animated:YES];
    
}



//实现代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self updateUI];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [_timer setFireDate:[NSDate distantFuture]];
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
}

@end
